package com.xiaoyi.ui.fragment.music;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avin.yi.R;
import com.xiaoyi.event.TabSelectedEvent;
import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.tool.http.XyApi;
import com.xiaoyi.ui.fragment.base.BaseMainFragment;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation.SupportFragment;


public class MusicFragment extends BaseMainFragment {


    public static MusicFragment newInstance() {
        Bundle args = new Bundle();

        MusicFragment fragment = new MusicFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBusActivityScope.getDefault(_mActivity).register(this);
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        initView(view, savedInstanceState);
        return view;
    }

    private void initView(View view, Bundle savedInstanceState) {
        Toolbar mToolbar =  view.findViewById(R.id.toolbar);
        mToolbar.setTitle("音乐欣赏");
        /// initToolbarNav(mToolbar);
      /*  if (findChildFragment(MusicListFragment.class) == null) {
            getMusicType();
        }*/
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

        if (findChildFragment(MusicListFragment.class) == null) {
            getMusicType("音乐");
        }
    }


    private void getMusicType(String mType){
        showLoadDialog2(_mActivity);
        XyApi.getInstance(_mActivity).getDataDirByType ( new SingleObserver<ArrayList<String>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onSuccess(ArrayList<String> strings) {
                MusicListFragment musicListFragment = MusicListFragment.newInstance(strings);
                loadRootFragment(R.id.fl_list_container, musicListFragment);
                // false:  不加入回退栈;  false: 不显示动画
                loadRootFragment(R.id.fl_content_container, MusicDetailFragment.newInstance(strings.get(0)), false, false);
                hideLoadDialog2();
            }

            @Override
            public void onError(Throwable e) {
                hideLoadDialog2();
                AvBaseTool.showMsg(_mActivity ,"错误："+ e.getMessage());
            }
        } ,mType);
    }


    @Override
    public boolean onBackPressedSupport() {
        super.onBackPressedSupport() ;
        return true;
    }

    /**
     * 替换加载 内容Fragment
     */
    public void switchContentFragment(MusicDetailFragment fragment) {
        SupportFragment contentFragment = findChildFragment(MusicDetailFragment.class);
        if (contentFragment != null) {
            contentFragment.replaceFragment(fragment, false);
        }
    }


    /**
     * 选择tab事件 . eventbus 需要，用来处理 双击 tab 事件
     */
    @Subscribe
    public void onTabSelectedEvent(TabSelectedEvent event) {
        AvBaseTool.showMsg(_mActivity ,"重复点击  " +event.position);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusActivityScope.getDefault(_mActivity).unregister(this);
    }
}
